<!--头部公共部分-->
{include file="common/header" /}
<div class="x-nav">
      <span class="layui-breadcrumb">
        <a><cite>课程:{$course_name}</cite></a>
        <a><cite>校区授权</cite></a>
      </span>
</div>

<div id="wodekehu" class="x-body" class="clear:both;">
    <div class="layui-row"
         style="padding-left:20px;border-left:1px solid #e5e5e5;border-right:1px solid #e5e5e5;">
        <div class="layui-form layui-col-md12 x-so layui-form-pane">
            <div class="layui-input-inline">
                <input type="text" name="campus_name" placeholder="输入校区名称筛选" autocomplete="off" class="layui-input">
                <input type="hidden" id="course_id" value="{$course_id}">
                <input type="hidden" id="course_name" value="{$course_name}">
            </div>
            <div class="layui-input-inline">
                <button class="layui-btn" id="search">
                    <i class="layui-icon layui-icon-search"></i>筛选</button>
                <button class="layui-btn layui-btn-danger" id="clear">
                    <i class="layui-icon"></i>重置</button>
            </div>
        </div>
    </div>
    <table class="layui-table" id="datalist" lay-filter="Event"></table>
</div>
<div class="layui-form-item">
    <div class="layui-input-block">
        <input class="layui-btn" id="wxsubmit" value="提交" readonly>
        <a class="layui-btn layui-btn-primary" id="fanhui">返回上级</a>
    </div>
</div>
<!--底部公共部分-->
{include file="common/footer" /}
<script>
    layui.use(['table', 'element', 'form', 'layer'], function () {
        var table = layui.table,
            form = layui.form,
            element = layui.element,
            layer = layui.layer,
            $ = layui.$;

        var loading = layer.load();
        var course_id = $("#course_id").val();
        var course_name = $("#course_name").val();

        //方法级渲染
        var tableIns = table.render({
            elem: '#datalist'
            , url: '{:url("Course/authorization")}?course_id=' + course_id
            , method: 'POST'
            , loading: true
            , parseData: function (res) { //res 即为原始返回的数据
                if (res.code === 1) {
                    res.code = 0;
                } else {
                    res.code = -1;
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.data //解析数据列表
                };
            },
            cols: [[
                {type: 'checkbox'},
                {field: 'campus_name',align: 'center', title: '校区名称'},
                {field: 'campus_price',align: 'center',edit: 'text', title: '<span title="每个校区都可以设置不同的授权单价，表示被授权校区默认只能以指定的价格来进行收费。">单价<sup>  <span class="layui-badge-dot" style="position: relative; top: -3px; left: -3px;"></span></sup></span>'},
            ]]
            , height: 'full-230'
            , page: false
            , done: function () {
                layer.close(loading);
            }
        });

        table.on('edit(Event)', function(obj){
            var value = obj.value //得到修改后的值
                ,data = obj.data //得到所在行所有键值
                ,field = obj.field //得到字段
            if(field === 'campus_price'){
                $.post('{:url("Course/campus_price_configure")}'
                    ,{
                        'campus_id':data.id,
                        'course_id':course_id,
                        'price':value
                    }
                    ,function(result){
                    if(result.code === 1){
                        location.reload();
                        $("#course_id").val(result.data);
                    }
                });
            }
        });

        $('#fanhui').click(function () {
            location.href = "{:url('Schedule/lists')}?id=" + course_id + '&course_name='+ course_name;
        });

        $('#wxsubmit').click(function () {
            var checkStatus = table.checkStatus('datalist'); //test即为基础参数id对应的值
            if (checkStatus.data.length == 0) {
                layer.msg('请至少选择一条数据！');
                return false;
            }
            var checked_ids = [];
            var file_id= 0;
            for (let index = 0; index < checkStatus.data.length; index++) {
                const element = checkStatus.data[index];
                checked_ids.push(element.id)
                file_id = element.file_id;
            }
            var data ={"ids":checked_ids.join(','),course_id:course_id};
            console.log(data);
            var url = "{:url('Course/addCampus')}";
            $.post(url,data,function(response){
                layer.msg(response.msg);
                if(response.code === 1){
                    location.href = "{:url('Course/lists')}";
                }
            });
        });


        //搜索项
        $('#search').click(function () {
            var field = {};
            field.campus_name = $('input[name=campus_name]').val();
            console.log(field);
            tableIns.reload({
                where: field,
                method: 'POST',
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
        //清除
        $('#clear').click(function () {
            $('.layui-form-pane').find('.layui-input-inline').each(function (index, element) {
                $(element).find('input[type=text]').val('');
                $(element).find('select').val('');
            })
            tableIns.reload({
                where: {
                    'campus_name': '',
                },
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });
    });
</script>

